package com.fbli.test.service.impl;

import com.fbli.test.common.entity.task.*;
import com.fbli.test.entity.Project;
import com.fbli.test.entity.Task;
import com.fbli.test.entity.User;
import com.fbli.test.mapper.TaskMapper;
import com.fbli.test.service.TaskService;
import com.fbli.test.util.IdUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class TaskServiceImpl implements TaskService {
    @Autowired
    TaskMapper taskMapper;
    @Autowired
    IdUtil idUtil;

    @Override
    public boolean addTask(TaskAddReqMsg taskAddReqMsg) {
        Task task=new Task();
        task.setName(taskAddReqMsg.getName());
        task.setProjectId(taskAddReqMsg.getProjectId());
        task.setEndTime(taskAddReqMsg.getEndTime());
        task.setStartTime(taskAddReqMsg.getStartTime());
        task.setCreatTime(System.currentTimeMillis());
        task.setState(0);
        task.setTaskId(idUtil.createTaskId());
        try {
            taskMapper.insert(task);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public boolean editTask(TaskEditReqMsg taskEditReqMsg) {
        Task task1 = taskMapper.selectByPrimaryKey(taskEditReqMsg.getId());
        if (task1==null) return false;
        Task task=new Task();
        task.setId(taskEditReqMsg.getId());
        task.setName(taskEditReqMsg.getName());
        task.setState(taskEditReqMsg.getState());
        task.setEndTime(taskEditReqMsg.getEndTime());
        task.setStartTime(taskEditReqMsg.getStartTime());
        try {
            taskMapper.updateByPrimaryKeySelective(task);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public List<Project> selectTask(TaskReqMsg taskReqMsg) {
        User user = (User) SecurityUtils.getSubject().getPrincipal();
        long userId = user.getId();
        List<Project> list = taskMapper.selectTask(userId, taskReqMsg.getProjectId());
        return list;
    }

    @Override
    public TaskPageResMsg selectAll(TaskPageReqMsg taskPageReqMsg) {
        User user = (User) SecurityUtils.getSubject().getPrincipal();
        long userId = user.getId();
        //开始分页
        PageHelper.startPage(taskPageReqMsg.getPage(),taskPageReqMsg.getSize());
        List<Task> tasks = taskMapper.selectAllTask(userId, taskPageReqMsg.getProjectId());
        //分页数据
        PageInfo<Task> taskPageInfo = new PageInfo<>(tasks);
        TaskPageResMsg taskPageResMsg=new TaskPageResMsg();
        taskPageResMsg.setTotal(taskPageInfo.getTotal());
        taskPageResMsg.setInfoList(taskPageInfo.getList());
        return taskPageResMsg;
    }

    @Override
    public List<Project> selectTaskShow(TaskReqMsg taskReqMsg) {
        User user = (User) SecurityUtils.getSubject().getPrincipal();
        long userId = user.getId();
        List<Project> list = taskMapper.selectTaskShow(userId,taskReqMsg.getProjectId());
        return list;
    }
}
